package com.example.wjy.viewmodels;

import android.app.Application;

import com.example.room.entity.MyJoinTeam;
import com.example.room.entity.Team;
import com.example.room.repository.MyJoinTeamRepository;
import com.example.room.repository.TeamRepository;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

/**
 * author: WangJunYan
 * package: com.example.wjy.viewmodels
 * date: 2020/6/6
 * time: 15:42
 * 用于处理MyJoinTeamFragment中的数据，例如从数据库中获取数据
 */
public class MyJoinTeamViewModel extends AndroidViewModel {

    /**
     * A list of teams from server.
     */
    MutableLiveData<List<MyJoinTeam>> teamList;

    MyJoinTeamRepository repository;

    public MyJoinTeamViewModel(@NonNull Application application) {
        super(application);
        repository = new MyJoinTeamRepository(application);
    }

    public LiveData<List<MyJoinTeam>> getTeamListLive() {
        return repository.getAllTeam();
    }

    public void deleteAllTeam() {
        repository.deleteAllTeam();
    }

    public void insertTeam(MyJoinTeam... joinTeam){
        repository.insertTeam(joinTeam);
    }

    /**
     * getter of teamList.
     *
     * @return The LiveData of teamList
     */
    public MutableLiveData<List<MyJoinTeam>> getTeamList() {
        if (teamList == null) {
            teamList = new MutableLiveData<>();
            getTeamListFromDatabase();
        }
        return teamList;
    }

    //用于测试能否在RecyclerView中显示数据，现在应该是没有用的
    public void getTeamListFromDatabase() {
        List<MyJoinTeam> teams = new ArrayList<>();
//        for (int i = 0;i < 20;i++){
//          Team team = new Team("wang","type");
//            teams.add(team);
//        }
        getTeamList().setValue(teams);
    }

}
